import * as React from 'react';
import { Menu, Form } from 'antd';
import validation from '../../utils/validation';
import IButton from '../../components/IButton';
import IForm from '../../components/IForm';
import http from '../../utils/http';
import auth from '../../utils/auth'

const IForms: {
    title: string,
    auth?: string[],
    IFormItems: any[],
}[] = [
        {
            title: "钱包设置",
            IFormItems: [{
                type: "text",
                name: "gasPrice",
                label: "账号2",
                validation: validation.required,
            }]
        },
        {
            title: "GW提现设置",
            auth: ["BOSS"],
            IFormItems: [{
                type: "text",
                name: "agreementRate",
                label: "账号1",
                validation: validation.required,
            }]
        },
    ]

let IFromDataType: any = {
    "activeCodeNo": "strValue"
}



class Advanced extends React.Component<{
    history: any,
    form: any,
}> {

    state: {
        current: string,
        IFormItems: any[],
        IFormData: any,
    } = {
            current: "0",
            IFormItems: IForms[0].IFormItems,
            IFormData: {},
        }


    componentDidMount() {
        this.getData();

    }

    async getData() {
        const { data } = await http.get("/admin/alls");

        let IFormData: any = {};
        data.data.map((item: any) => {
            const type = IFromDataType[item.keyName] || "value";
            IFormData[item.keyName] = item[type];
        });
        console.log(IFormData);
        this.setState({
            IFormData: IFormData,
        });
    }

    handleClick(e: any) {
        this.props.form.resetFields();
        this.setState({
            current: e.key,
            IFormItems: IForms[e.key].IFormItems,
        });
    }


    async handleSubmit() {
        const value = await validation.validateFields(this.props.form);
        console.log(value);
    }

    public render() {
        return <>
            <Menu
                selectedKeys={[this.state.current]}
                onClick={this.handleClick.bind(this)}
                mode="horizontal"
                inlineIndent={0}
            >
                {
                    IForms.map((item, index) => {
                        return auth(item.auth) ?
                            <Menu.Item key={index}>
                                {item.title}
                            </Menu.Item> : null;
                    })
                }

            </Menu>

            <div style={{
                maxWidth: 540,
            }}>
                <IForm
                    {...this.props}
                    items={this.state.IFormItems}
                    data={this.state.IFormData}
                />
                <IButton name="确定" type="primary" onClick={this.handleSubmit.bind(this)} />
            </div>

        </>
    }
}

export default Form.create({ name: 'Advanced' })(Advanced);;